﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MinhTuan;
using Products;

public partial class shoping_Checkout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string user = Session["user"].ToString();
            if (int.Parse(user.Split(',')[0]) < 0)
            {
                Response.Redirect("http://localhost:2011/didong/users/dangnhap.html");
            }
            else
            {   
                Showmember();
                String cart = Session["Shopcart"].ToString();
                SelectedList mySelectedItems = SelectedList.GetSelectedItems(cart);
                showproduc(mySelectedItems);

            }

        }
    }
    public void Showmember()
    {
        String[] user = Session["user"].ToString().Split(',');
        Members mymember = Members.GetMemberinfo(int.Parse(user[0]));
        hdid.Value = mymember.Members_Id.ToString();
        lname.Text = mymember.Members_Login;
        cname.Text = mymember.Members_Name;
        caddress.Text = mymember.Members_Address;
        cemail.Text = mymember.Members_Mail;
        ccity.Text = mymember.Members_City;
        ctel.Text = mymember.Members_Tel;
     
         
    }
    public void showproduc( SelectedList mySelectedItems)
    { 
        if (mySelectedItems.Count > 0)
        {
            String cart = "Shopcart";
            this.products.DataSource = mySelectedItems.Items;
            this.products.DataBind();
            cart = SelectedList.UpdateShoppingcart(mySelectedItems);
            Session["Shopcart"] = cart;
            Literal fQuantity = (Literal)(this.products.FindControl("fQuantity"));
            Literal fTotal = (Literal)(this.products.FindControl("fTotal"));
            Literal litHeader = (Literal)(this.products.FindControl("litHeader"));
            Literal litBottom = (Literal)(this.products.FindControl("litBottom"));
            if (fTotal != null && fQuantity != null)
            {
                fQuantity.Text = String.Format("{0:0,00}", mySelectedItems.Quantity);
                fTotal.Text = String.Format("{0:0,00}", mySelectedItems.Total);
                Session["count"] = mySelectedItems.Quantity.ToString();
            }
            if (litHeader != null && litBottom != null)
            {
               // SelectedList.BuildTitle(litHeader, litBottom,"Vn");
            }
        }
        else
        {
           // Response.Redirect("http://localhost:2011/didong/Shoping/emptycart.vienthongsaigon");
        }
   
    }
    protected void BtnOrder_Click(object sender, EventArgs e)
    {
        if (string.Compare(Session["strcapcha"].ToString(), icapcha.Text.ToLower(), true) == 0)
        {
            if (this.products.Items.Count > 0)
            {
                ShoppingCart mycart = new ShoppingCart();
                mycart.UserId = Convert.ToInt16(hdid.Value.ToString());
                mycart.ShippingCity = ccity.Text;
                mycart.ShippingName = cname.Text;
                mycart.ShippingAddress = caddress.Text;
                mycart.ShippingTel =ctel.Text;
                mycart.OrderDate = DateTime.Now;
                mycart.OrderNotice = "true";
                mycart.Items.Clear();
                String[] cartinfo;
                CartItem myCartItem = new CartItem();
                HiddenField hdinfo;
                foreach (ListViewItem myItem in this.products.Items)
                {
                    if (myItem.ItemType == ListViewItemType.DataItem)
                    {
                        hdinfo = (HiddenField)myItem.FindControl("hdinfo");
                        if (hdinfo != null)
                        {
                            cartinfo = hdinfo.Value.ToString().Split(',');
                            myCartItem = new CartItem(Convert.ToInt32(cartinfo[0]), cartinfo[1], Convert.ToInt32(cartinfo[2]), Convert.ToDecimal(cartinfo[3]), cartinfo[4]);
                            mycart.Items.Add(myCartItem); 
                            
                        }
                    }
                }
               
                int OrderId = ShoppingCart.AddNewOrder(mycart);
              
                lblError.Visible = true;
                lblError.Text = OrderId.ToString();
               
                if (OrderId != -1)
                {
                   lblError.Visible = true;
                    Session["Shopcart"] = null;
                    Response.Redirect("http://localhost:2011/didong/finished/" + OrderId + "/" + mycart.Items.Count + "/" + mycart.Quantity + "/" + mycart.Total + ".html");
                }
                else {

                    lblError.Text = " Don hang that bai";
                    lblError.Visible = true;
                }
              
            }
        }
        else
        {
            lblError.Visible=true;
           
        }
    }
}
